import re,pyperclip

phoneRegex = re.compile(
    r'''(
    (\d{3}|\(\d{3}\))?                # area code      [1]
    (\s|-|\.)?                        # separator      [2]
    (\d{3})                           # first 3 digits [3]
    (\s|-|\.)                         # separator      [4]
    (\d{4})                           # last 4 digits  [5]
    (\s*(ext|x|ext.)\s*(\d{2,5}))?    # extension 整体0或1次[8?]
    )''', re.VERBOSE)
# mo = phoneRegex.search(r'800.420.7240')
# print(mo.group())

emailRegex = re.compile(
    r'''(
    [a-zA-Z]+            # 用户名
    @                    # @符号
    [a-zA-Z0-9.-]+       # 域名
    (\.[a-zA-Z]{2,4})    # .com等
    )'''
    ,re.VERBOSE)
# mo = emailRegex.search(r'ac@no.com')
# print(mo.group())


text = str(pyperclip.paste())
# print(text)
matches = []

for groups in phoneRegex.findall(text):
    # 想取ext.|ext|x怎么写？groups[?]
    print('g[1]' + groups[1])
    print('g[7]' + groups[7])
    print('g[8]' + groups[8])
    phoneNum = '-'.join([groups[1], groups[3], groups[5]])
    if groups[8] != '':
        phoneNum += ' x' + groups[8]
    matches.append(phoneNum)
    print()

for groups in emailRegex.findall(text):
    matches.append(groups[0])
   # print(groups[0])
   # print()

if len(matches) > 0:
    pyperclip.copy('\n'.join(matches))
    print('---Copied to clipboard---')
    print('\n'.join(matches))
else:
    print('No phone numbers or email addresses found.')
